package PhantomJS;

import cn.hutool.core.util.URLUtil;
import com.github.jarlakxen.embedphantomjs.ExecutionTimeout;
import com.github.jarlakxen.embedphantomjs.PhantomJSReference;
import com.github.jarlakxen.embedphantomjs.executor.PhantomJSFileExecutor;
import org.junit.Test;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.util.concurrent.TimeUnit;

/**
 * @program JavaBase
 * @description:
 *  https://github.com/Jarlakxen/embedphantomjs
 *  https://phantomjs.org/page-automation.html
 *  https://github.com/ariya/phantomjs/blob/master/examples/page_events.js
 *  https://phantomjs.org/api/command-line.html
 * @author: zhanglu
 * @create: 2021-03-04 14:37:00
 */
public class Test01 {

    /**
     * 字符串转html文件
     * @param htmlContent
     * @return
     */
    public static String stringToHtml(String htmlContent,String code){
        String fileName = code + ".html";
        StringBuffer localRoute = new StringBuffer("/Users/zhanglu/Desktop/lu/workspace/dev_workspace/idea/学习/JavaBase/JavaBase/src/main/java/PhantomJS/" ) ;
        localRoute.append(fileName);
        File newFile = new File(localRoute.toString());
        if (!newFile.getParentFile().exists()) {
            newFile.getParentFile().mkdirs();
        }
        try {
            newFile.createNewFile();
            PrintStream printStream = new PrintStream(new FileOutputStream(newFile),true,"utf-8");//todo:编码问题
            printStream.println(htmlContent);//将字符串写入文件
        } catch (IOException e) {
            e.printStackTrace();

        }
        return localRoute.toString();
    }

    @Test
    public void test01() throws Exception {
//        String htmlContent = "<html style=\"margin:0;padding:0;\"><body style=\"margin:0;padding:0;\"><div style=\"width:600px;height:450px;\"><div data-v-6e872718=\"\" id=\"canvas-panel\" class=\"page-preview-wrapper\" style=\"height: 450px; background-size: cover; background-color: rgb(0, 255, 0);\"><div data-v-50e542a5=\"\" data-v-6e872718=\"\" class=\"components-edit-shape active\" style=\"width: 53px; height: 22px; left: 369px; top: 226px; position: absolute;\"><div data-v-50e542a5=\"\" class=\"edit-shape-point\" style=\"margin-left: -5px; margin-top: -5px; left: 0px; top: 0px; cursor: nw-resize;\"></div><div data-v-50e542a5=\"\" class=\"edit-shape-point\" style=\"margin-left: -5px; margin-top: -5px; left: 53px; top: 0px; cursor: ne-resize;\"></div><div data-v-50e542a5=\"\" class=\"edit-shape-point\" style=\"margin-left: -5px; margin-top: -5px; left: 0px; top: 22px; cursor: sw-resize;\"></div><div data-v-50e542a5=\"\" class=\"edit-shape-point\" style=\"margin-left: -5px; margin-top: -5px; left: 53px; top: 22px; cursor: se-resize;\"></div><div data-v-50e542a5=\"\" class=\"edit-shape-point\" style=\"margin-left: -5px; margin-top: 0px; left: 0px; top: 11px; cursor: w-resize;\"></div><div data-v-50e542a5=\"\" class=\"edit-shape-point\" style=\"margin-left: -5px; margin-top: 0px; left: 53px; top: 11px; cursor: e-resize;\"></div><div data-v-50e542a5=\"\" class=\"edit-shape-point\" style=\"margin-left: 0px; margin-top: -5px; left: 26.5px; top: 0px; cursor: n-resize;\"></div><div data-v-50e542a5=\"\" class=\"edit-shape-point\" style=\"margin-left: 0px; margin-top: -5px; left: 26.5px; top: 22px; cursor: s-resize;\"></div><div data-v-853061da=\"\" data-v-6e872718=\"\" class=\"zdm-text-placeHolder element-on-edit-pane\" data-v-50e542a5=\"\" style=\"position: absolute; width: 53px; height: 22px; top: 0px; left: 0px; padding: 0px; margin: 0px; border-width: 0px; border-style: solid; border-radius: 0px; font-size: 16px; font-weight: 500; line-height: 1.4; letter-spacing: 0px; text-align: center; color: rgb(0, 0, 0); background-size: cover; opacity: 1; z-index: 1; transform: rotate(0deg);\"> ${test} </div></div><div data-v-50e542a5=\"\" data-v-6e872718=\"\" class=\"components-edit-shape\" style=\"width: 105px; height: 27px; left: 238px; top: 176px; position: absolute;\"><div data-v-671d3426=\"\" data-v-6e872718=\"\" class=\"zdm-text element-on-edit-pane\" data-v-50e542a5=\"\" style=\"position: absolute; width: 105px; height: 27px; top: 0px; left: 0px; padding: 0px; margin: 0px; border-width: 0px; border-style: solid; border-radius: 0px; font-size: 16px; font-weight: 500; line-height: 1.4; letter-spacing: 0px; text-align: center; color: rgb(0, 0, 0); background-size: cover; opacity: 1; z-index: 2; transform: rotate(0deg);\"> 这是一段文字 </div></div></div></div></body></html>";
//        System.out.println(htmlContent);
        //<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        String url = "file:///Users/zhanglu/Desktop/lu/workspace/dev_workspace/idea/学习/JavaBase/JavaBase/src/main/java/PhantomJS/2.html";
        url = URLUtil.encode(url);
//        url = "http://61.147.13.132:8088/upload/html/20210303154246555782436.html";
        String html = "var page = require('webpage').create();" +
                "page.open('"+url+"', function() {" +
//                "    sleep(10);" +
                "    var ua = page.evaluate(function() {" +
                "      return document.getElementById('qua').textContent;" +
                "    });" +
                "    console.log(ua);console.log(page.content);console.log(page.render('/Users/zhanglu/Desktop/lu/workspace/dev_workspace/idea/学习/JavaBase/JavaBase/src/main/java/PhantomJS/PhantomJS.png'));" +
                "    phantom.exit();" +
                "});" +
                "" +
                "function sleep(delay) {" +
                "    var start = (new Date()).getTime();" +
                "    while ((new Date()).getTime() - start < delay) {" +
                "        continue;" +
                "    }" +
                "}";
        System.out.println(html);
        PhantomJSReference phantomJSRef = PhantomJSReference.create().build();
        PhantomJSFileExecutor ex = new PhantomJSFileExecutor(phantomJSRef, new ExecutionTimeout(30, TimeUnit.SECONDS));
        String s = ex.execute(html,  "--local-to-remote-url-access=true").get();
        System.out.println(s);
        if(s.equals("true")){
            System.out.println("图片生成成功");
        }else {
            System.out.println("图片生成失败");
        }
    }


}
